package com.lovecanon.jzjl.utils;





import org.apache.poi.sl.usermodel.Sheet;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

public class ExportExcelUtil {

    public Workbook getWorkBook(String[] title, List<? extends Object[]> data) throws FileNotFoundException, IOException {

        // 创建工作簿
        Workbook wb = new XSSFWorkbook();
        //创建字体
        XSSFFont redFont = (XSSFFont)wb.createFont();
        //设置字体颜色为红色
        redFont.setColor(XSSFFont.COLOR_RED);
        // 创建一个工作表sheet
        org.apache.poi.ss.usermodel.Sheet sheet = null;
        // 行
        Row row = null;
        // 单元格
        Cell cell = null;
        // 单元格样式
        CellStyle titleStyle = wb.createCellStyle();
        CellStyle  cellStyle = wb.createCellStyle();
        // 字体样式
        Font font = wb.createFont();
        // 粗体
        //font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
        titleStyle.setFont(font);
        // 水平居中
        titleStyle.setAlignment(HorizontalAlignment.CENTER);
        // 垂直居中
        titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 水平居中
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        // 垂直居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 行数据
        Object[] rowData = null;
        // 设置自动换行
        cellStyle.setWrapText(true);

        // 创建工作表
        sheet = wb.createSheet();
        row = sheet.createRow(0);

        // 写入标题
        for(int i = 0; i < title.length; i++){
            cell = row.createCell(i);
            cell.setCellStyle(titleStyle);
            cell.setCellValue(title[i].toString());
        }

        int[] maxWidth = new int[data.get(0).length];
        // 写入行数据
        for(int rowNumber = 0; rowNumber < data.size(); rowNumber++){
            // 如果没有标题栏，起始行就是0，如果有标题栏，行号就应该为1
            row = sheet.createRow(title == null ? rowNumber : (rowNumber + 1));
            rowData = data.get(rowNumber);

            for(int columnNumber = 0; columnNumber < rowData.length; columnNumber++){
                cell = row.createCell(columnNumber);
                cell.setCellStyle(cellStyle);

                if(rowData[columnNumber] == null) {
                    cell.setCellValue("");
                }
                else {
                    cell.setCellValue(rowData[columnNumber].toString());
                }

                int amount = 0;
                int width = 0;
                if(rowData[columnNumber] != null){
                    width = ((rowData[columnNumber].toString().length()-amount)*256)+(amount*512);	//所需列宽，数字英文*256，中文*512
                }
                if(width > maxWidth[columnNumber]){	//保留最大列宽
                    maxWidth[columnNumber] = width;
                }else {
                    width = maxWidth[columnNumber];
                }
                try {
                    sheet.setColumnWidth(columnNumber, width+1000);	//宽度+500，单元格两边留空隙
                } catch (Exception e) {
                    //内容太长造成宽度设置异常，默认设置一个定死的宽度
                    sheet.setColumnWidth(columnNumber, 6000);
                }
            }
        }

        return wb;
    }



}
